# -*— coding: utf-8 -*-
# @Time: 2024/5/16 15:22
# @Author :Lin Zhou
# @Email : lin1013143@163.com
# @File : convert_json_keys_to_snake_case.py
# @SoftWare: PyCharm
import re


def camel_to_snake(name):
    s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\g<2>', name)
    return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()


def convert_json_keys_to_snake_case(json_dict):
    """
    将json格式中的驼峰命名法的key转为下划线命名法
    :param json_dict:
    :return:
    """
    if not isinstance(json_dict, dict):
        return json_dict
    return {camel_to_snake(key): convert_json_keys_to_snake_case(value) for key, value in json_dict.items()}
